package com.mingmen.canting.caigourenyuan;


import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mingmen.canting.R;
import com.mingmen.canting.adapter.SecondAdapter;
import com.mingmen.canting.model.ShoppingCartModel;
import com.mingmen.canting.model.SimpleData;

import java.util.ArrayList;
import java.util.List;


/**
 *
 */
public class CaiGouRenDaiCaiAdapter extends RecyclerView.Adapter<CaiGouRenDaiCaiAdapter.ViewHolder> {
    //新增itemType
    public static final int ITEM_TYPE = 100;

    private Context mContext;
    private List<ShoppingCartModel.DataBean.SupDataListBean> mNewList = new ArrayList<>();

    //定义一个点击事件的接口
    public interface OnitemClick {
        void onItemClick(View view, int parentPosition, int position, List<ShoppingCartModel.DataBean.SupDataListBean.DetailsBean> mList, List<ShoppingCartModel.DataBean.SupDataListBean> mNewList);
    }

    private OnitemClick onitemClick;   //定义点击事件接口

    public void setOnitemClickLintener(OnitemClick onitemClick) {
        this.onitemClick = onitemClick;
    }

//    private List<ShoppingCartModel.DataBean.SupDataListBean.DetailsBean> list = new ArrayList<>();

    public CaiGouRenDaiCaiAdapter(Context context, List<ShoppingCartModel.DataBean.SupDataListBean> mNewList) {
        mContext = context;
        this.mNewList = mNewList;
    }

    public void setDataList(List<ShoppingCartModel.DataBean.SupDataListBean> mNewList) {


        this.mNewList = mNewList;

        Log.d("mNewListsize", mNewList.size() + "");

        notifyDataSetChanged();
    }

    //重写改方法，设置ItemViewType
    @Override
    public int getItemViewType(int position) {
        //返回值与使用时设置的值需保持一致
        return ITEM_TYPE;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_caigoudandai, parent, false);
        return new ViewHolder(view);
    }

    int flag = 0;

    public void seleceAll(int flag) {
        this.flag = flag;
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.text_name.setText(mNewList.get(position).getSupplierName());

        if (mNewList.get(position).getSupplierName().equals("")) {
            holder.img_sanjiao.setVisibility(View.GONE);
            holder.text_name.setVisibility(View.GONE);
        } else {
            holder.img_sanjiao.setVisibility(View.VISIBLE);
            holder.text_name.setVisibility(View.VISIBLE);
        }
        Log.d("position", position + "");

        if (holder.mRvAdapter == null) {
//            holder.mRvAdapter = new CaiGouDanZiAdapter(mContext, holder.list, position);
            holder.mRvAdapter = new CaiGouDanZiAdapter(mContext, mNewList.get(position).getDetails(), position);
            LinearLayoutManager layoutManage = new LinearLayoutManager(mContext);
            holder.rvItemItem.setLayoutManager(layoutManage);
//            holder.rvItemItem.addItemDecoration(new GridSpacingItemDecoration(2, 20, false));
            holder.rvItemItem.setAdapter(holder.mRvAdapter);
        } else {
            holder.mRvAdapter.setPosition(position);
            holder.mRvAdapter.notifyDataSetChanged();

        }

        int parentPosition = position;

   /*     if (flag == 2) {
            holder.checkbox_groupitem.setChecked(true);
        } else {
            holder.checkbox_groupitem.setChecked(false);
        }*/
        holder.checkbox_groupitem.setChecked(mNewList.get(position).isSelect());

        holder.mRvAdapter.setOnitemClickLintener(new CaiGouDanZiAdapter.OnitemClick() {
            @Override
            public void onItemClick(View view, int position, List<ShoppingCartModel.DataBean.SupDataListBean.DetailsBean> mList) {

                switch (view.getId()) {

                    case R.id.checkbox:
                        childposition=position;
                        mNewList.get(parentPosition).setSelect(!mNewList.get(parentPosition).isSelect());
//                        onitemClick.onItemClick(view, parentPosition, holder.getAdapterPosition(), mList, mNewList);
                        onitemClick.onItemClick(view, parentPosition, position, mList, mNewList);
//                        holder.mRvAdapter.notifyDataSetChanged();
                        break;
                    case R.id.text_fangqi:
                        childposition=position;
                        onitemClick.onItemClick(view, parentPosition, position, mList, mNewList);
                        break;
                }

            }
        });
        holder.checkbox_groupitem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("listsize", mNewList.get(position).getDetails().size() + "");

                if (onitemClick != null) {
                    mNewList.get(parentPosition).setSelect(!mNewList.get(parentPosition).isSelect());
                    onitemClick.onItemClick(v, position, childposition, mNewList.get(position).getDetails(), mNewList);
//                    holder.mRvAdapter.notifyDataSetChanged();
                }

              /*  if (mNewList.get(position).isSelect() == true) {
                    for (int i = 0; i < mNewList.get(position).getDetails().size(); i++) {
                        mNewList.get(position).getDetails().get(i).setSelect(true);
                    }
                    mNewList.get(position).setSelect(true);
                } else {
                    for (int i = 0; i < mNewList.get(position).getDetails().size(); i++) {
                        mNewList.get(position).getDetails().get(i).setSelect(false);
                    }
                    mNewList.get(position).setSelect(false);
                }*/
            }
        });
        holder.layout_selectshouhuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onitemClick != null) {
//                    onitemClick.onItemClick(v, position, position, list, mNewList);
//                    onitemClick.onItemClick(v, position, holder.getAdapterPosition(), mNewList.get(position).getDetails(), mNewList);


                    onitemClick.onItemClick(v, position,childposition, mNewList.get(position).getDetails(), mNewList);
                }
            }
        });
        holder.lable_selectcanting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onitemClick != null) {
                    onitemClick.onItemClick(v, position,childposition, mNewList.get(position).getDetails(), mNewList);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return mNewList == null ? 0 : mNewList.size();
    }
    int childposition;
    static class ViewHolder extends RecyclerView.ViewHolder {

        RecyclerView rvItemItem;

        private CaiGouDanZiAdapter mRvAdapter;

        TextView text_name;
        CheckBox checkbox_groupitem;
        LinearLayout layout_selectshouhuo;
        LinearLayout lable_selectcanting;
        TextView sele_gongYing;
        ImageView img_sanjiao;

        ViewHolder(View view) {
            super(view);
            text_name = view.findViewById(R.id.text_name);

            rvItemItem = view.findViewById(R.id.rv_item);
            checkbox_groupitem = view.findViewById(R.id.checkbox_groupitem);
            layout_selectshouhuo = view.findViewById(R.id.layout_selectshouhuo);
            lable_selectcanting = view.findViewById(R.id.lable_selectcanting);
            sele_gongYing = view.findViewById(R.id.sele_gongYing);
            img_sanjiao = view.findViewById(R.id.img_sanjiao);
        }
    }
}